package utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileFilter {
    private final ICollect collect;
    private final boolean target;
    private final boolean justFirstLevelDir;    //只看第一级的文件夹,不看深层文件夹
    private final boolean collectHideFile;      //是否需要收集隐藏文件

    public static List<FileBean> filter(boolean target, String path, ICollect collect) {
        return filter(target, false, false, path, collect);
    }

    public static List<FileBean> filter(boolean target, boolean justFirstLevelDir, boolean collectHideFile, String path, ICollect collect) {
        if (TextUtils.isEmpty(path)) {
            return null;
        } else {
            if (collect == null) {
                collect = name -> true;
            }
            FileFilter filter = new FileFilter(target, justFirstLevelDir, collectHideFile, collect);
            return filter.filter(path);
        }
    }

    public static void delEmptyDir(String path) {
        if (!TextUtils.isEmpty(path)) {
            File parent = new File(path);
            if (parent.exists()) {
                if (parent.isDirectory()) {
                    File[] dirs = parent.listFiles();
                    if (dirs != null) {
                        if (empty(parent)) {
                            for (File file : dirs) {//删除内部隐藏的无效文件
                                boolean success = file.delete();
                                log(String.format("%s 删除：%s", file.getName(), success ? "成功" : "失败"));
                            }
                            boolean success = parent.delete();
                            log(String.format("文件夹%s 为空，删除操作：%s", path, success ? "成功" : "失败"));
                            if (success) {
                                File parentFile = parent.getParentFile();
                                if (parentFile.exists() && parentFile.isDirectory()) {
                                    delEmptyDir(parentFile.getAbsolutePath());
                                }
                            }
                        } else {
                            for (File dir : dirs) {
                                delEmptyDir(dir.getAbsolutePath());
                            }
                        }
                    }
                }
            }
        } else {
            log("path为空！");
        }
    }

    private static boolean empty(File parent) {
        File[] dirs = parent.listFiles();
        if (dirs != null && dirs.length != 0) {
            for (File file : dirs) {
                if (file.isDirectory() || (file.isFile() && !file.isHidden())) {
                    return false;
                }
            }
        }
        return true;
    }

    private FileFilter(boolean target, boolean justFirstLevelDir, boolean collectHideFile, ICollect collect) {
        this.target = target;
        this.justFirstLevelDir = justFirstLevelDir;
        this.collectHideFile = collectHideFile;
        this.collect = collect;
    }

    private List<FileBean> filter(String path) {
        List<FileBean> items = new ArrayList<>();
        File file = new File(path);
        if (file.exists() && notHideFile(file)) {
            if (justFirstLevelDir) {
                File[] files = file.listFiles();
                if (files != null && files.length > 0) {
                    for (File item : files) {
                        if (item.exists() && notHideFile(item) && item.isDirectory()) {
                            if (collect.needCollect(item.getName())) {
                                FileBean bean = FileBean.newInstance(target, item);
                                items.add(bean);
                            }
                        }
                    }
                }
            } else {//看所有文件
                if (!file.isDirectory()) {
                    if (collect.needCollect(file.getName())) {
                        FileBean bean = FileBean.newInstance(target, file);
                        items.add(bean);
                    }
                } else {
                    File[] files = file.listFiles();
                    if (files != null && files.length > 0) {
                        for (File subItem : files) {
                            List<FileBean> subPaths = filter(subItem.getPath());
                            items.addAll(subPaths);
                        }
                    }
                }
            }
        }
        return items;
    }

    private static void log(String content) {
        LogUtil.log(content);
    }

    private boolean notHideFile(File file) {
        return collectHideFile || !file.isHidden();
    }

    public interface ICollect {
        boolean needCollect(String name);
    }
}
